
var _article = $("#article");
//上传图片
function upload() {
    _article.blur()
    uploadHeadPic(function (res) {
        var msgImg = $("#msgImg");
        msgImg.show();
        msgImg.find('img').attr({
            'src': ajaxUrl + res.path,
            'data-id': res.icon
        });
        msgImg_id = res.icon;
        msgImg.find('.delete').show();
        // icons.push(res.icon)
    })
}

//删除图片
function removePic(that,event){
    event.stopPropagation();
    var msgImg = $("#msgImg");
    var _this = $(that);
    _this.siblings('img').attr('src','').removeAttr('data-id');
    _this.hide();
    msgImg.hide();
    msgImg_id = 0;
}

//视频上传
function showVideo() {
    _article.blur()
    api.actionSheet({
        title: '上传视频',
        cancelTitle: '取消',
        buttons: ['拍摄','选择视频']
    }, function (ret, err) {
        if (ret) {
            getVideo(ret.buttonIndex);
        }
    });
}

function getVideo(sourceType) {
    if (sourceType == 2) { // 从相机中选择
        var ISphoen = api.systemType;
        var UIAlbumBrowser = api.require('UIAlbumBrowser');
        UIAlbumBrowser.open({
            max: 1,
            type: 'video',
            isOpenPreview: false,
            classify: false,
            styles: {
                bg: '#fff',
                mark: {
                    icon: '',
                    position: 'bottom_left',
                    size: 20
                },
                nav: {
                    bg: 'rgba(0,0,0,0.6)',
                    titleColor: '#fff',
                    titleSize: 18,
                    cancelColor: '#fff',
                    cancelSize: 16,
                    finishColor: '#fff',
                    finishSize: 16
                }
            },
            rotation: true
        }, function (ret) {
            // alert(JSON.stringify(ret));
            if (ret.eventType == "cancel") {
                pop.notice("取消了操作！")
                return;
            }
            var dt = ret.list;

            api.showProgress({
                title: '正在上传...',
                modal: true
            });
            if (ret) {
                if (ISphoen == "ios") {
                    // for (var i in dt) {

                    UIAlbumBrowser.transPath({
                        path: ret.list[0].path
                    }, function (ret, err) {
                        if (ret) {
                            // alert(JSON.stringify(ret));
                            console.log(ret.path)
                            var ListVideoSec = ret.path;
                            api.ajax({
                                url: ajaxUrl + Interface.upload_file,
                                method: 'post',
                                data: {
                                    files: {
                                        'file': ret.path,
                                    }
                                }
                            }, function (ret, err) {
                                console.log(JSON.stringify(ret))
                                if (ret) {

                                    if (ret.code == 200) {
                                        //arrimg.push(ret)
                                        api.hideProgress();
                                        addVideos(ret.icon, ajaxUrl + ret.path, ListVideoSec);
                                    } else {
                                        api.hideProgress();
                                        api.toast({
                                            msg: ret.msg,
                                            location: 'middle'
                                        });
                                    }
                                } else {

                                    api.hideProgress();
                                    api.toast({
                                        msg: err.msg,
                                        location: 'middle'
                                    });
                                }
                            });
                        }
                    });
                    // }
                } else {
                    var ListVideoSec = ret.list[0].path;
                    // for (var i in dt) {
                    api.ajax({
                        url: ajaxUrl + Interface.upload_file,
                        method: 'post',
                        data: {
                            files: {
                                'file': ret.list[0].path,
                            }
                        }
                    }, function (ret, err) {
                        console.log(JSON.stringify(ret))
                        if (ret) {

                            if (ret.code == 200) {
                                //arrimg.push(ret)
                                api.hideProgress();
                                addVideos(ret.icon, ajaxUrl + ret.path, ListVideoSec);
                            } else {
                                api.hideProgress();
                                api.toast({
                                    msg: ret.msg,
                                    location: 'middle'
                                });
                            }
                        } else {
                            api.hideProgress();
                            api.toast({
                                msg: err.msg,
                                location: 'middle'
                            });
                        }
                    });


                    // }
                }
            }
        });

    } else if (sourceType == 1) {
        //拍摄视频
        api.getPicture({
            sourceType: 'camera',
            encodingType: 'png',
            mediaValue: 'video',
            allowEdit: false,
            videoQuality: 'high',
            destinationType: 'url',
            saveToPhotoAlbum: true
        }, function (ret, err) {
            var ISphoen = api.systemType;
            if (ISphoen == "ios") {
                if (ret == '') {
                    return;
                }
            } else {
                if (ret.data == '') {
                    return;
                }
            }
            if (ret) {

                var imgSrc = ret.data;
                if (imgSrc != "") {

                    //$api.attr(ele, 'src', imgSrc);
                }
                api.showProgress({
                    title: '正在上传...',
                    modal: true
                });
                // api.hideProgress();
                var ele = $api.dom('#paishe');
                api.ajax({
                    url: ajaxUrl + Interface.upload_file,
                    method: 'post',
                    data: {
                        files: {
                            'file': ret.data,
                        }
                    }
                }, function (ret, err) {
                    //  alert(JSON.stringify(ret));
                    if (ret) {
                        if (ret.code == 200) {

                            api.hideProgress();
                            addVideos(ret.icon, ajaxUrl + ret.path, imgSrc);

                        } else {
                            api.hideProgress();
                            api.toast({
                                msg: ret.msg,
                                location: 'middle'
                            });
                        }
                    } else {

                        api.hideProgress();
                        api.toast({
                            msg: err.msg,
                            location: 'middle'
                        });
                    }
                });


            }
        });
    }
}

function addVideos(id, videoPath, bdvideopath) {

    var videoScreenshots = api.require('videoScreenshots');
    var timestamp = new Date().getTime();
    videoScreenshots.screenshots({
        videoUrl: bdvideopath,//视频url
        time: 1,
        name: timestamp
    }, function (ret) {
        if (ret.status == true) {
            console.log(JSON.stringify(ret));
            video_id = id;//上传之后的视频id

            comment_add()
        }
    });
}
